<%--
  Created by IntelliJ IDEA.
  User: 10161
  Date: 2018/12/27
  Time: 16:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>新闻列表</title>
    <meta charset="UTF-8">

    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/js/jquery-1.11.0.js"></script>
    <script type="text/javascript">

        function editNews(id){
//            var title=$("input[name='title"+id+"']");
            var title=$("#title"+id).val();
            $.ajax({
                url:"${pageContext.request.contextPath}/news/edit",
                data:{id:id,title:title},
                dataType:"text",
                type:"POST",
                success:function(result){
                    if(result=='1'){
                        alert("更新数据成功！");
                    }else{
                        alert("更新数据失败！");
                    }
                    window.location=window.location;
                },
                error:function(){
                    alert("对不起！服务器正忙!");
                    window.location=window.location;
                }
            });
        }


        function deleteNews(id) {
            $.get("/news/dodelete",{id:id},function (result) {
                if (!confirm("删除类别会删除所有其子类，你确定要删除嘛！")){
                    return ;
                }
                if(result=="1"){
                    alert("删除成功");
                }else {
                    alert("删除失败");

                }
                window.location= window.location;
            });
        }

        $(function () {
            $("#cbAll").click(function () {
                $("input[name='cbItem']").prop("checked", $("#cbAll").prop("checked"));
            });
        });
        function deleteAllNews () {
            if (!confirm("删除类别会删除所有其子类，你确定要删除嘛！")){
                return ;
            }
            var ids ="-1";
            $("input[name='cbItem']").each(function () {
                if(this.checked){
                    var id= $(this).val();
                    ids+=","+id;
                }
            });
            $.get("${pageContext.request.contextPath }/news/dodeleteall",{ids:ids},function (result) {
                if(result=="1"){
                    alert("批量删除成功");
                }else {
                    alert("批量删除失败");
                }
                window.location= window.location;
            });
        }
    </script>
</head>
<body>
<form action="${pageContext.request.contextPath}/news/list" method="post">
    <table>
        <tr>
            <td>新闻标题:<input type="text" name="title" value="${news.title}"/></td>
            <td>新闻类别:<select name="typeId">
                <option value="0">==不限==</option>
                <c:forEach var="item" items="${plist}">
                    <option <c:if test="${item.id==news.typeId}">selected="selected"</c:if>
                            value="${item.id}">${item.typeName}</option>
                </c:forEach>
            </select> </td>
            <td>开始日期:<input type="text" name="createDate1" value="${news.createDate1}"></td>
            <td>结束日期:<input type="text" name="createDate2" value="${news.createDate2}"></td>
            <td>
                <input type="submit" value="搜索">&nbsp;
                <a href="javascript:void(0)" onclick="deleteAllNews()">批量删除</a>
            </td>
            <td>类别名称</td>
        </tr>
    </table>
</form>
<table>
    <tr>
        <td><input type="checkbox" id="cbAll"/>ID</td>
        <td>新闻标题</td>
        <td>新闻类别</td>
        <td>作者</td>
        <td>创建日期</td>
        <td>操作</td>
    </tr>
    <c:forEach items="${list}" var="li">
        <tr>
            <td><input type="checkbox" name="cbItem" value="${li.id}"/>${li.id}</td>
            <td><input type="text" id="title${li.id}" name="title${li.id}" value="${li.title}"/></td>
            <td>${li.typeName}</td>
            <td>${li.author}</td>
            <td><fmt:formatDate pattern="yyyy-MM-dd HH:mm" value="${li.createDate}"/></td>
            <td><a href="javascript:void(0)" onclick="editNews(${li.id});">更新</a>
                <a href="${pageContext.request.contextPath}/news/update?id=${li.id}">修改</a>
                <a href="javascript:void(0)" onclick="deleteNews(${li.id});">删除</a></td>
        </tr>
    </c:forEach>
</table>
<p>共${pageTool.totalRecordCount}条数据，当前第${pageTool.currPageNo}/${pageTool.pageCount}页，每页${pageTool.pageSize}条，


    <a href="${pageContext.request.contextPath }/news/list?title=${news.title}&typeId =${news.typeId}&createDate1=${news.createDate1}&createDate2=${news.createDate2}&currPageNo=1">首页</a>

    <a href="${pageContext.request.contextPath }/news/list?title=${news.title}&typeId=${news.typeId}&createDate1=${news.createDate1}&createDate2=${news.createDate2}&currPageNo=${pageTool.currPageNo-1}">前一页</a>

    <c:forEach var="prePage" items="${prePages}">
        <a href="${pageContext.request.contextPath }/news/list?title=${news.title}&typeId=${news.typeId}&createDate1=${news.createDate1}&createDate2=${news.createDate2}&currPageNo=${pageTool.currPageNo-1}">${prePage}</a>
    </c:forEach>

    <b>${pageTool.currPageNo}</b>

    <c:forEach var="nextPage" items="${nextPages}">
        <a href="${pageContext.request.contextPath }/news/list?title=${news.title}&typeId=${news.typeId}&createDate1=${news.createDate1}&createDate2=${news.createDate2}&currPageNo=${pageTool.currPageNo+1}">${nextPage}</a>
    </c:forEach>


    <a href="${pageContext.request.contextPath }/news/list?title=${news.title}&typeId=${news.typeId}&createDate1=${news.createDate1}&createDate2=${news.createDate2}&currPageNo=${pageTool.currPageNo+1}">下一页</a>

    <a href="${pageContext.request.contextPath }/news/list?title=${news.title}&typeId=${news.typeId}&createDate1=${news.createDate1}&createDate2=${news.createDate2}&currPageNo=${pageTool.pageCount}">末页</a>

</p>

</body>
</html>
